package com.leo.basic.scheduler.dao;

import com.leo.basic.scheduler.bean.SchedulerJob;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author DELL
 */
@Mapper
public interface SchedulerJobMapper {

    @Insert({"insert into ${schedulerJObTableName} (JOB_ID, CREATE_TIME, UPDATE_TIME, JOB_NAME, JOB_GROUP, JOB_STATUS, CRON_EXPRESSION," +
            "DESCRIPTION, BEAN_CLASS, IS_CONCURRENT, SPRING_ID, METHOD_NAME, PARENTJOBID, BATCH_STATUS, BATCH_TIME, TRIGGER_TYPE," +
            "PARAMETER_JSON, START_DATE, END_DATE) values (#{jobId,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP}," +
            "#{jobName,jdbcType=VARCHAR},#{jobGroup,jdbcType=VARCHAR},#{jobStatus,jdbcType=VARCHAR},#{cronExpression,jdbcType=VARCHAR}," +
            "#{description,jdbcType=VARCHAR},#{beanClass,jdbcType=VARCHAR},#{isConcurrent,jdbcType=VARCHAR},#{springId,jdbcType=VARCHAR}," +
            "#{methodName,jdbcType=VARCHAR},#{parentJobId,jdbcType=VARCHAR},#{batchStatus,jdbcType=VARCHAR},#{batchTime,jdbcType=VARCHAR}," +
            "#{triggerType,jdbcType=VARCHAR},#{parameterJson,jdbcType=VARCHAR},#{startDate,jdbcType=VARCHAR},#{endDate,jdbcType=VARCHAR})"})
    void insertSelective(String schedulerJObTableName, SchedulerJob schedulerJob);

    @SelectProvider(type = SchedulerJobSqlProvider.class, method = "getScheduleJobByWhere")
    @Results({
            @Result(column = "JOB_ID", property = "jobId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "JOB_NAME", property = "jobName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_GROUP", property = "jobGroup", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_STATUS", property = "jobStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CRON_EXPRESSION", property = "cronExpression", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DESCRIPTION", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BEAN_CLASS", property = "beanClass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IS_CONCURRENT", property = "isConcurrent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SPRING_ID", property = "springId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "METHOD_NAME", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENTJOBID", property = "parentJobId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_STATUS", property = "batchStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_TIME", property = "batchTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "TRIGGER_TYPE", property = "triggerType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARAMETER_JSON", property = "parameterJson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "START_DATE", property = "startDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "END_DATE", property = "endDate", jdbcType = JdbcType.VARCHAR)
    })
    List<SchedulerJob> getSchedulerJobByWhere(@Param("schedulerJObTableName") String schedulerJObTableName, @Param("record") SchedulerJob schedulerJob);

    @SelectProvider(type = SchedulerJobSqlProvider.class, method = "selectByPrimaryKey")
    @Results({
            @Result(column = "JOB_ID", property = "jobId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "JOB_NAME", property = "jobName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_GROUP", property = "jobGroup", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_STATUS", property = "jobStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CRON_EXPRESSION", property = "cronExpression", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DESCRIPTION", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BEAN_CLASS", property = "beanClass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IS_CONCURRENT", property = "isConcurrent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SPRING_ID", property = "springId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "METHOD_NAME", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENTJOBID", property = "parentJobId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_STATUS", property = "batchStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_TIME", property = "batchTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "TRIGGER_TYPE", property = "triggerType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARAMETER_JSON", property = "parameterJson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "START_DATE", property = "startDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "END_DATE", property = "endDate", jdbcType = JdbcType.VARCHAR)
    })
    SchedulerJob selectByPrimaryKey(String schedulerJObTableName, String jobId);

    @Update({"update ${schedulerJObTableName} set CREATE_TIME=#{createTime,JdbcType.TIMESTAMP},UPDATE_TIME=#{updateTime,JdbcType.TIMESTAMP},JOB_NAME=#{jobName,JdbcType.VARCHAR},\n" +
            "JOB_GROUP=#{jobGroup,JdbcType.VARCHAR},JOB_STATUS=#{jobStatus,JdbcType.VARCHAR},CRON_EXPRESSION=#{cronExpression,JdbcType.VARCHAR},\n" +
            "DESCRIPTION=#{description,JdbcType.VARCHAR},BEAN_CLASS=#{beanClass,JdbcType.VARCHAR},IS_CONCURRENT=#{isConcurrent,JdbcType.VARCHAR},\n" +
            "SPRING_ID=#{springId,JdbcType.VARCHAR},METHOD_NAME=#{methodName,JdbcType.VARCHAR},PARENTJOBID=#{parentJobId,JdbcType.VARCHAR},\n" +
            "BATCH_STATUS=#{batchStatus,JdbcType.VARCHAR},BATCH_TIME=#{batchTime,JdbcType.VARCHAR},TRIGGER_TYPE=#{triggerType,JdbcType.VARCHAR},\n" +
            "PARAMETER_JSON=#{parameterJson,JdbcType.VARCHAR},START_DATE=#{startDate,JdbcType.VARCHAR},END_DATE=#{endDate,JdbcType.VARCHAR) where JOB_ID = #{jobId,JdbcType.VARCHAR}"})
    void updateByPrimaryKeySelective(String schedulerJObTableName, SchedulerJob schedulerJob);

    @SelectProvider(type = SchedulerJobSqlProvider.class, method = "getAllScheduleJob")
    @Results({
            @Result(column = "JOB_ID", property = "jobId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "JOB_NAME", property = "jobName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_GROUP", property = "jobGroup", jdbcType = JdbcType.VARCHAR),
            @Result(column = "JOB_STATUS", property = "jobStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CRON_EXPRESSION", property = "cronExpression", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DESCRIPTION", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BEAN_CLASS", property = "beanClass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IS_CONCURRENT", property = "isConcurrent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SPRING_ID", property = "springId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "METHOD_NAME", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENTJOBID", property = "parentJobId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_STATUS", property = "batchStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BATCH_TIME", property = "batchTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "TRIGGER_TYPE", property = "triggerType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARAMETER_JSON", property = "parameterJson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "START_DATE", property = "startDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "END_DATE", property = "endDate", jdbcType = JdbcType.VARCHAR)
    })
    List<SchedulerJob> getAllScheduleJob(String schedulerJObTableName, @Param("record") SchedulerJob schedulerJob);

    @Delete({"delete from ${schedulerJObTableName} where JOB_ID = #{jobId,JdbcType.VARCHAR}"})
    void deleteByPrimaryKey(String schedulerJObTableName, String jobId);
}
